package com.pets.web.job;

import com.alibaba.fastjson2.JSONObject;
import com.pets.common.utils.StringUtils;
import com.pets.web.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Objects;

@Slf4j
@Component
public class JsapiTicketJob {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${wx.globalTicket}")
    private String globalTicket;

    public void refreshJsTicket(){
        String accessToken = redisTemplate.opsForValue().get(Constant.ACCESS_TOKEN);
        String url = String.format(globalTicket, accessToken);
        JSONObject result = restTemplate.getForObject(url, JSONObject.class);
        Objects.requireNonNull(result, "请求微信js_ticket返回为空！");
        String ticket = result.getString("ticket");
        log.info("------------->刷新js_ticket: {}", ticket);
        redisTemplate.opsForValue().set(Constant.JS_TICKET, ticket);
    }
}
